#-*- coding: utf-8 -*-
import requests
import time
import hashlib
import base64

URL = "http://openapi.xfyun.cn/v2/aiui"
APPID = "5d5a6180"
API_KEY = "c6ec06ff0b10411bfd2039bf49e5d044"
AUE = "raw"
AUTH_ID = "08d6d234b440afdae52c9f5f351ba64c"
DATA_TYPE = "audio"
SAMPLE_RATE = "16000"
SCENE = "main"
RESULT_LEVEL = "complete"
LAT = "39.938838"
LNG = "116.368624"
#个性化参数，需转义
PERS_PARAM = "{\\\"auth_id\\\":\\\"2894c985bf8b1111c6728db79d3479ae\\\"}"
FILE_PATH = r"/Users/ddy/Music/Apowersoft Audio Recorder/test.wav"


def buildHeader():
    curTime = str(int(time.time()))
    param = "{\"result_level\":\"" + RESULT_LEVEL + "\","\
            + "\"auth_id\":\"" + AUTH_ID + "\","\
            + "\"data_type\":\"" + DATA_TYPE + "\","\
            + "\"sample_rate\":\"" + SAMPLE_RATE + "\","\
            + "\"scene\":\"" + SCENE + "\","\
            + "\"lat\":\"" + LAT + "\","\
            + "\"lng\":\"" + LNG + "\"}"
    print(param)
    #使用个性化参数时参数格式如下：
    #param = "{\"result_level\":\""+RESULT_LEVEL+"\",\"auth_id\":\""+AUTH_ID+"\",\"data_type\":\""+DATA_TYPE+"\",\"sample_rate\":\""+SAMPLE_RATE+"\",\"scene\":\""+SCENE+"\",\"lat\":\""+LAT+"\",\"lng\":\""+LNG+"\",\"pers_param\":\""+PERS_PARAM+"\"}"
    paramBase64 = base64.b64encode(param.encode())

    m2 = hashlib.md5()
    str_s = API_KEY + curTime + paramBase64.decode('utf-8')
    print(str_s)
    m2.update(str_s.encode('utf-8'))
    checkSum = m2.hexdigest()

    header = {
        'X-CurTime': curTime,
        'X-Param': paramBase64,
        'X-Appid': APPID,
        'X-CheckSum': checkSum,
    }
    print(header)
    return header

def readFile(filePath):
    binfile = open(filePath, 'rb')
    data = binfile.read()
    return data

r = requests.post(URL, headers=buildHeader(), data=readFile(FILE_PATH))
print(r.content)
